/*
 * Jp_ListeTechniciens.java
 *
 * Created on 4 mai 2008, 12:27
 */

package gui;

import IBO.ITechnicien;
import boerrors.CodePostalMinimumMultiplicityException;
import boerrors.ZoneMinimumMultiplicityException;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  olivierm
 */
public class Jp_ListeTechniciens extends javax.swing.JPanel {
    
    private Frm_Application _parent = null;
    private DefaultTableModel _modele = new DefaultTableModel();
    private String[] _colName = {"Matricule", "Nom", "Prénom", "Zone", "Fonction", "Téléphone", "Email", "Rue", "Numéro", "Code Postal", "Commune", "Zone"};
    private Vector<ITechnicien> _listeTEchniciens = null;
    
    public void fillFields(){
        try {
            this._modele.setRowCount(0);
            _listeTEchniciens = this._parent.getFactoryRmi().getGestionEmployes().listerTechniciens();
            String[] obj = new String[13];
            for (int i = 0; i < _listeTEchniciens.size(); i++){
                obj[0] = String.valueOf(_listeTEchniciens.get(i).getMatricule());
                obj[1] = _listeTEchniciens.get(i).getNom();
                obj[2] = _listeTEchniciens.get(i).getPrenom();
                obj[3] = _listeTEchniciens.get(i).getIZone().getLibelleZone();
                obj[4] = _listeTEchniciens.get(i).getFonction();
                obj[5] = _listeTEchniciens.get(i).getTelephone();
                obj[6] = _listeTEchniciens.get(i).getEmail();
                obj[7] = _listeTEchniciens.get(i).getIAdresse().getRue();
                obj[8] = _listeTEchniciens.get(i).getIAdresse().getNumero();
                obj[10] = _listeTEchniciens.get(i).getIAdresse().getICodePostal().getCodePostal();
                obj[11] = _listeTEchniciens.get(i).getIAdresse().getICodePostal().getVille();
                obj[12] = _listeTEchniciens.get(i).getIAdresse().getICodePostal().getZone().getLibelleZone();
                this._modele.addRow(obj);
            }
            jlbl_NbrElements.setText(String.valueOf(this._modele.getRowCount()));
        } catch (CodePostalMinimumMultiplicityException ex) {
            Logger.getLogger(Jp_ListeTechniciens.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ZoneMinimumMultiplicityException ex) {
            Logger.getLogger(Jp_ListeTechniciens.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(Jp_ListeTechniciens.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void initComponents(Frm_Application parent){
        this._parent = parent;
        
    }
    
    /** Creates new form Jp_ListeTechniciens */
    public Jp_ListeTechniciens() {
        initComponents();
        this._modele.setColumnIdentifiers(_colName);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jXStatusBar1 = new org.jdesktop.swingx.JXStatusBar();
        jLabel1 = new javax.swing.JLabel();
        jlbl_NbrElements = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jbtn_Fermer = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel7.setBackground(new java.awt.Color(0, 102, 153));
        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel11.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("Liste des techniciens");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addContainerGap(246, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addContainerGap())
        );

        add(jPanel7, java.awt.BorderLayout.NORTH);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/car.png"))); // NOI18N
        jButton1.setText("Details");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);
        jToolBar1.add(jSeparator1);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/database_refresh.png"))); // NOI18N
        jButton2.setText("Rafraichir");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jPanel1.add(jToolBar1, java.awt.BorderLayout.NORTH);

        jTable1.setModel(_modele);
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jLabel1.setText("Nombre d'élément(s): ");
        jXStatusBar1.add(jLabel1);
        jXStatusBar1.add(jlbl_NbrElements);

        jPanel1.add(jXStatusBar1, java.awt.BorderLayout.SOUTH);

        add(jPanel1, java.awt.BorderLayout.CENTER);

        jbtn_Fermer.setText("Fermer");
        jbtn_Fermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_FermerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(323, Short.MAX_VALUE)
                .addComponent(jbtn_Fermer)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jbtn_Fermer)
        );

        add(jPanel2, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtn_FermerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_FermerActionPerformed

        this.setVisible(false);
        
    }//GEN-LAST:event_jbtn_FermerActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        fillFields();
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try{
            Jd_DetailsTechnicien obj = new Jd_DetailsTechnicien(_parent, true);
            ITechnicien tech = this._listeTEchniciens.elementAt(jTable1.getSelectedRow());
            obj.initComponents2(_parent, tech);
            obj.setVisible(true);            
        } catch (NullPointerException ex){
            JOptionPane.showMessageDialog(this,"Veuillez sélectionner un technicien.","Information",JOptionPane.INFORMATION_MESSAGE);
        } catch (ArrayIndexOutOfBoundsException ex){
            JOptionPane.showMessageDialog(this,"Veuillez sélectionner un technicien.","Information",JOptionPane.INFORMATION_MESSAGE);
        }

        
    }//GEN-LAST:event_jButton1ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private org.jdesktop.swingx.JXStatusBar jXStatusBar1;
    private javax.swing.JButton jbtn_Fermer;
    private javax.swing.JLabel jlbl_NbrElements;
    // End of variables declaration//GEN-END:variables
    
}
